package com.example.examplebitmappool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.examplebitmappool.test.IBitmapPoolTest;
import com.example.examplebitmappool.test.impl.BitmapImplTestImpl;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements Runnable {

    private ImageView mIv;
    private InputStream inputStream;
    private Bitmap bitmap;
    private Bitmap realBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    public void action(View view) {
        path = "http://pic.netbian.com/uploads/allimg/190824/212516-1566653116f355.jpg";
        //开启子线程
        new Thread(this).start();
    }

    public void action2(View view) {
        path = "http://pic.netbian.com/uploads/allimg/200410/213246-1586525566c099.jpg";
        new Thread(this).start();
    }

    public void action3(View view) {
        path = "http://pic.netbian.com/uploads/allimg/201228/234048-160917004816a8.jpg";
//        path = "https://cn.bing.com/sa/simg/hpb/LaDigue_EN-CA1115245085_1920x1080.jpg";
        new Thread(this).start();
    }

    IBitmapPoolTest mBitmapPoolTest = new BitmapImplTestImpl(1024 * 1024 * 6);
    private static final String TAG = "MainActivity";
    private String path = "http://pic.netbian.com/uploads/allimg/190824/212516-1566653116f355.jpg";

    @Override
    public void run() {
        try {
            /**
             * 1.获取流
             */
            URL url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(5000);
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
                /**
                 * 2.获取复用bitmap，可能有也可能没有
                 */
                int h=1920;
                int w=1080;
                bitmap = mBitmapPoolTest.getBitmap(w,h,Bitmap.Config.RGB_565);
                /**
                 * 3.设置bitmap选项的属性
                 */
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inBitmap = bitmap;//复用
                options.inJustDecodeBounds = false;//表示不只获得宽高
                options.inMutable = true;//表示可变，这样才可以复用bitmap
                /**
                 * 4.根据属性获取真正bitmap
                 */
                realBitmap = BitmapFactory.decodeStream(inputStream, null, options);
                /**
                 * 5.bitmap储存到复用池，也可能不储存
                 */
                mBitmapPoolTest.putBitmap(realBitmap);
//                Bitmap.createBitmap(realBitmap,)
                /**
                 * 6.子线程更新UI，设置图片
                 */
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mIv.setImageBitmap(realBitmap);
                    }
                });
            } else {
                Log.d(TAG, "run responseCode: " + responseCode);
            }

        } catch (Exception e) {
            Log.d(TAG, "run: e:" + e.getMessage());
        }finally {
            if (inputStream!=null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.d(TAG, "run: e:"+e.getMessage());
                }
            }
            /*if (bitmap!=null) {
                bitmap.recycle();
            }*/
            /*if (realBitmap!=null) {
                realBitmap.recycle();
            }*/
        }
    }

    private void initView() {
        mIv = (ImageView) findViewById(R.id.iv);
    }
}